/*
 * Copyright 2011 yura.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.opu.db_vdumper.util.ui;

import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;
import javax.swing.text.JTextComponent;
import org.opu.db_vdumper.actions.AbstractAction;

/**
 *
 * @author yura
 */
public class GUITextUtils {

    public static final String CUT = "Cut";
    public static final String COPY = "Copy";
    public static final String PASTE = "Paste";

    // Suppresses default constructor, ensuring non-instantiability.
    private GUITextUtils() {
    }

    public static <T extends JTextComponent> T addActions(final T textComponent) {
        if (textComponent == null) {
            return null;
        }

        final AbstractAction cut = getAction(CUT, new Runnable() {

            @Override public void run() {
                textComponent.cut();
            }
        });

        AbstractAction copy = getAction(COPY, new Runnable() {

            @Override public void run() {
                textComponent.copy();
            }
        });

        final AbstractAction paste = getAction(PASTE, new Runnable() {

            @Override public void run() {
                textComponent.paste();
            }
        });
        
        final JPopupMenu menu = new JPopupMenu();
        menu.add(cut);
        menu.add(copy);
        menu.add(paste);

//        textComponent.add(menu);
//        textComponent.setComponentPopupMenu(menu);

        textComponent.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent evt) {
                if (SwingUtilities.isLeftMouseButton(evt)) {
                    final JTextComponent fld = (JTextComponent) evt.getSource();
                } else if (SwingUtilities.isRightMouseButton(evt)) {
                    boolean edit = textComponent.isEditable()
                            && textComponent.isEnabled();
                    cut.setEnabled(edit);
                    paste.setEnabled(edit);
                    menu.show(evt.getComponent(), evt.getX(), evt.getY());
                }
            }
        });

        return textComponent;
    }

    public static AbstractAction getAction(final String name, final Runnable run) {
        return new AbstractAction() {

            @Override
            public String getName() {
                return name;
            }

            @Override
            public void action(ActionEvent e) {
                run.run();
            }
        };
    }
}
